﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace SRF.Web
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        static SRF.Common.Logging.ILog dataLogger = SRF.Common.Logging.LogManager.GetLogger("SRF.Data");

        protected static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        protected static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                new string[] { "SRF.Web.Controllers.*" }
            ); 
        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas(); 

            Initialize();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }

        public static object GetParamValue(string paraName)
        {
            if (paraName.StartsWith(SRF.Security.DataRule.DataRuleProvider.CodePrefix))  //数据过滤规则
            {
                return SRF.Security.SecurityContext.GetDataRuleCondition(paraName);
            }
            else
            {
                switch (paraName)
                {
                    case "user.id":
                        return SRF.Security.SecurityContext.User == null ? null : SRF.Security.SecurityContext.User.Id;
                    case "user.loginid":
                        return SRF.Security.SecurityContext.User == null ? null : SRF.Security.SecurityContext.User.LoginId;
                    case "user.username":
                        return SRF.Security.SecurityContext.User == null ? null : SRF.Security.SecurityContext.User.UserName;
                }
            }

            return null;
        }

        /// <summary>
        /// 初始化
        /// </summary>
        public static void Initialize()
        {
            ValueProviderFactories.Factories.Add(new SRF.UI.JsonArrayValueProviderFactory());
            ModelValidatorProviders.Providers.RemoveAt(2);  //删除数据类型 的校验  

            SRF.Data.Cfg.Configuration.WriteLog = msg =>
            {
                dataLogger.Debug(msg);
            };
            // 数据库查询默认参数
            SRF.Data.Query.GetParamValue = GetParamValue;
            //登陆
            SRF.Security.SecurityContext.LoginCompleted += () =>
            {
                //写日志
                SRF.Common.Logging.ILoginLogger loginLogger = SRF.Common.ObjectHelper.GetObject<SRF.Common.Logging.ILoginLogger>();
                if (loginLogger != null)
                    loginLogger.Write(System.Web.HttpContext.Current.Request.RequestContext.HttpContext.Request, SRF.Security.SecurityContext.User == null ? "" : SRF.Security.SecurityContext.User.Id);
            };
            SRF.Security.SecurityContext.LogoutCompleted += () =>
            {
            };

            //获取当前用户
            SRF.Common.Logging.LogAttribute.GetCurrentUserId =
                () => SRF.Security.SecurityContext.User == null ? "" : SRF.Security.SecurityContext.User.Id;

            //--
            RouteTable.Routes.MapRoute(
                "Login",
                "{action}/{id}",
                new { controller = "Home", id = UrlParameter.Optional },
                new { action = "Login|Logout|Install" },
                new string[] { "SRF.Web.Controllers.*" }
            );

        }
    }
}